<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 站点demo
 *
 * admin-SiteDemo
 *
 * @Apidoc\Title("站点demo")
 * @Apidoc\Group("api")
 */
class SiteDemo extends BaseController
{
    /**
     * 新增
     *
     * admin-SiteDemo-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            //如果有核心接口文件，必须要验证里面是否包含{aidogkey}字符，并且需要包含两次（一次是上面的define，一次是下面repalce中要替换的）
            if($data['site_demo__core_interface_file']){
                $count= $this->checkKeyStr($data['site_demo__core_interface_file']); 
                if(!$count){
                    return error("核心接口文件需要包含{aidogkey}");
                }
            }
            $data = group_by_table_name($data);
            \app\common\model\SiteDemo::create($data['site_demo']);
            return success();
        } else {
            return View::fetch();
        }
    }
    //验证是否包含两次{aidogkey}字符，并且需要包含两次（一次是上面的define，一次是下面repalce中要替换的）
    private function checkKeyStr($filepath){
        //禁用ssl
        $contextOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false
            )
        );  
        $contextOptions = stream_context_create($contextOptions);      
        $file=file_get_contents($filepath, false, $contextOptions);
        $count = substr_count($file, "{aidogkey}");
        return $count;
    }
    /**
     * 修改
     *
     * admin-SiteDemo-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            //如果有核心接口文件，必须要验证里面是否包含{aidogkey}字符，并且需要包含两次（一次是上面的define，一次是下面repalce中要替换的）
            if($data['site_demo__core_interface_file']){
                $count= $this->checkKeyStr($data['site_demo__core_interface_file']); 
                if(!$count){
                    return error("核心接口文件需要包含{aidogkey}");
                }
            }            
            $data = group_by_table_name($data);
            \app\common\model\SiteDemo::update($data['site_demo']);
            return success();
        } else {
            $data = \app\common\model\SiteDemo::find(input('site_demo__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['site_demo__' . $key] = $val;
            }
            View::assign('r', $r);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-SiteDemo-delete
     */
    public function delete()
    {
        \app\common\model\SiteDemo::destroy(input('site_demo__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-SiteDemo-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_site_demo');
        if (input('site_demo__name', '') !== '') {
            $query->where('db_site_demo.name', 'like', '%' . input('site_demo__name', '', 'search_in') . '%');
        }
        $query->where('db_site_demo.delete_time', '=', null);
        $query->field(['db_site_demo.id' => 'site_demo__id']);
        $query->field(['db_site_demo.name' => 'site_demo__name']);
        $query->field(['db_site_demo.sort' => 'site_demo__sort']);
        $query->field(['db_site_demo.column_interface' => 'site_demo__column_interface']);
        $query->field(['db_site_demo.publishing_interface' => 'site_demo__publishing_interface']);
        $query->field(['db_site_demo.logo_image' => 'site_demo__logo_image']);
        $query->order('db_site_demo.sort', 'asc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        foreach($data['data'] as $key=>$val){
            $data['data'][$key]['site_num']=Db::name("site")->where("site_demo_id",$val["site_demo__id"])->count();
        }  
        View::assign($data);
        $options = [];
        $site_demo__id_option = \app\common\model\SiteDemo::column('name as name, id as id');
        $options['site_demo__id_options'] = $site_demo__id_option;
        View::assign($options);
        return View::fetch();
    }
}
